<?php $form = $this->beginWidget('GxActiveForm', 
	array(
		'id' => 'firms-form',
		'enableAjaxValidation' => false,
		'htmlOptions'=>array(
			'enctype' => 'multipart/form-data',
			'class'=>'form-horizontal',
		),
	));
?>

<?php if(Yii::app()->user->hasFlash('user_myfirms_msg')): ?>
<p class="text-success"><?php echo Yii::app()->user->getFlash('user_myfirms_msg'); ?></p>
<?php endif; ?>

<div class="control-group">
<p class="note"><?php echo Yii::t('app', 'Поля'); ?> <span class="required">*</span> <?php echo Yii::t('app', 'обязательные'); ?>.</p>
</div>


<?php if (null == $model->id): ?>
<div class="control-group">
	<?php echo $form->labelEx($model,'firm_tmpl_id',array('class'=>'control-label')); ?>
	<div class="controls">
		<?php echo $form->dropDownList($model, 'firm_tmpl_id', GxHtml::listDataEx(FirmTmpls::model()->findAllAttributes(null, true, 'hidden=0'))); ?>
		<span class="help-inline"><?php echo $form->error($model,'firm_tmpl_id'); ?></span>
	</div>
</div>
<?php else : ?>
<div class="control-group">
	<?php echo $form->labelEx($model,'hidden',array('class'=>'control-label')); ?>
	<div class="controls">
		<i><?php echo $model->hidden ? Yii::t('app', 'Да') : Yii::t('app', 'Нет');?></i>
		<span class="help-inline">
			<a href="<?php echo Yii::app()->createUrl('user/myfirms/hidden', array('id'=>$model->id, 'hidden' => $model->hidden ? 0 : 1)); ?>">
				<?php echo $model->hidden ? UserModule::t('Сделать видимой') : UserModule::t('Скрыть'); ?>
			</a>
		</span>		
	</div>
</div>
<div class="control-group">
	<?php echo $form->labelEx($model,'firm_tmpl_id',array('class'=>'control-label')); ?>
	<div class="controls">
		<i><?php echo GxHtml::encode($model->firmTmpl);?></i>
		<span class="help-inline">
			<a href="<?php echo Yii::app()->createUrl('user/myfirms/changeplan', array('id'=>$model->id)); ?>">
				<?php echo UserModule::t('Изменить пакет'); ?>
			</a>
		</span>
	</div>
</div>
<?php endif;?>
<div class="control-group">
	<?php echo $form->labelEx($model,'service_type',array('class'=>'control-label')); ?>
	<div class="controls">
		<?php  
            echo $form->dropDownList($model, 'service_type', UserController::getServiceTypes(),
	            array(
	                'ajax' => array(
	                    'type'=>'POST', //request type
	                    'url'=> $this->createUrl('/user/default/getrubrics'),
	                    'update'=>'#profile_rubrics',
	                    'data'=>array('id'=>'js:this.value'),
	                    'success' => 'function(html) {
	                            jQuery("#profile_rubrics").html(html);
	                            jQuery("#profile_rubrics").change();
	                    }'                      
	                )
	            )
            );
        ?>
		<span class="help-inline"><?php echo $form->error($model,'service_type'); ?></span>
	</div>
</div>
<div class="control-group">
	<?php echo $form->labelEx($model,'rubric_id',array('class'=>'control-label')); ?>
	<div class="controls">
		<?php 
            $service_type = (null != $model->service_type) ? $model->service_type : 0; 
			echo $form->dropDownList($model, 'rubric_id', GxHtml::listDataEx(Rubrics::model()->visible()->servicetype($service_type)->findAllAttributes(null, true)),
	            array(
	                'id' => 'profile_rubrics',
	                'ajax' => array(
	                    'type'=>'POST',
	                    'url'=> $this->createUrl('/user/default/getcategories'),
	                    'data'=>array('id'=>'js:this.value'),
						'update'=>'#profile_categories',
	                )
	            )
			);
		?>
		<span class="help-inline"><?php echo $form->error($model,'rubric_id'); ?></span>
	</div>
</div>
<div class="control-group">
	<?php echo $form->labelEx($model,'category_id',array('class'=>'control-label')); ?>
	<div class="controls">
		<?php 
			$rubric_id = 0 < $model->rubric_id ? $model->rubric_id : Yii::app()->params['default_rubric_id'];
			echo $form->dropDownList($model, 'category_id', 
				GxHtml::listDataEx(Categories::model()->visible()->rubric_id($rubric_id)->servicetype($service_type)->findAllAttributes(null, true)), 
				array('id'=>'profile_categories')
			);
		?>
		<span class="help-inline"><?php echo $form->error($model,'category_id'); ?></span>
	</div>
</div>
<div class="control-group">
	<?php echo $form->labelEx($model,'title',array('class'=>'control-label')); ?>
	<div class="controls">
		<?php echo $form->textField($model, 'title', array('maxlength' => 150,'placeholder'=>UserModule::t('Заголовок'))); ?>
		<span class="help-inline"><?php echo $form->error($model,'title'); ?></span>
	</div>
</div>
<div class="control-group">
	<?php echo $form->labelEx($model,'description',array('class'=>'control-label')); ?>
	<div class="controls">
		<?php echo $form->textArea($model, 'description',array('rows'=>6,'placeholder'=>UserModule::t('Описание'))); ?>
		<span class="help-inline"><?php echo $form->error($model,'description'); ?></span>
	</div>
</div>
<div class="control-group">
	<?php echo $form->labelEx($model,'region_id',array('class'=>'control-label')); ?>
	<div class="controls">
		<?php 
			$country_id = 0 < $model->country_id ? $model->country_id : Yii::app()->params['default_country_id'];
			echo $form->dropDownList($model, 'region_id', 
				GxHtml::listDataEx(Regions::model()->visible()->findAllAttributes(null, true, 'hidden = 0 AND country_id = :cid', array(':cid' => $country_id))), 
				array(
                    'id'=>'profile_regions',
                    'ajax' => array(
                        'type'=>'POST',
                        'url'=> $this->createUrl('/user/default/getcities'),
                        'update'=>'#profile_cities', 
                        'data'=>array('id'=>'js:this.value'),
                    ),
                )
			);
		?>
		<span class="help-inline"><?php echo $form->error($model,'region_id'); ?></span>
	</div>
</div>
<div class="control-group">
	<?php echo $form->labelEx($model,'city_id',array('class'=>'control-label')); ?>
	<div class="controls">
		<?php 
			$region_id = 0 < $model->region_id ? $model->region_id : Yii::app()->params['default_region_id'];
			echo $form->dropDownList($model, 'city_id', 
				GxHtml::listDataEx(Cities::model()->visible()->findAllAttributes(null, true, 'hidden = 0 AND region_id = :rid', array(':rid' => $region_id))), 
				array('id'=>'profile_cities')
			);
		?>
		<span class="help-inline"><?php echo $form->error($model,'city_id'); ?></span>
	</div>
</div>
<div class="control-group">
	<?php echo $form->labelEx($model,'address',array('class'=>'control-label')); ?>
	<div class="controls">
		<?php echo $form->textField($model, 'address', array('maxlength' => 150,'placeholder'=>UserModule::t('Адрес'))); ?>
		<span class="help-inline"><?php echo $form->error($model,'address'); ?></span>
	</div>
</div>
<div class="control-group">
	<?php echo $form->labelEx($model,'zip',array('class'=>'control-label')); ?>
	<div class="controls">
		<?php echo $form->textField($model, 'zip', array('maxlength' => 15,'placeholder'=>UserModule::t('Индекс'))); ?>
		<span class="help-inline"><?php echo $form->error($model,'zip'); ?></span>
	</div>
</div>
<div class="control-group">
	<?php echo $form->labelEx($model,'email',array('class'=>'control-label')); ?>
	<div class="controls">
		<?php echo $form->textField($model, 'email', array('maxlength' => 100,'placeholder'=>UserModule::t('Контактный email'))); ?>
		<span class="help-inline"><?php echo $form->error($model,'email'); ?></span>
	</div>
</div>
<div class="control-group">
	<?php echo $form->labelEx($model,'contact_fio',array('class'=>'control-label')); ?>
	<div class="controls">
		<?php echo $form->textField($model, 'contact_fio', array('maxlength' => 100,'placeholder'=>UserModule::t('Контактные ФИО'))); ?>
		<span class="help-inline"><?php echo $form->error($model,'contact_fio'); ?></span>
	</div>
</div>
<div class="control-group">
	<?php echo $form->labelEx($model,'phone',array('class'=>'control-label')); ?>
	<div class="controls">
		<?php echo $form->textField($model, 'phone', array('maxlength' => 100,'placeholder'=>UserModule::t('Контактный телефон'))); ?>
		<span class="help-inline"><?php echo $form->error($model,'phone'); ?></span>
	</div>
</div>
<?php if (0 < $model->id && $model->firmTmpl && $model->firmTmpl->logo_flag): ?>
<div class="control-group">
	<?php echo $form->labelEx($model,'logo',array('class'=>'control-label')); ?>
	<div class="controls">
		<?php if ('' != $model->logo): ?>
			<div id="img_block">
				<?php
					echo GxHtml::image($this->getFirmImgSrc($model->logo, 'firms_img_logo'), 'logo', array());
					$imgajax = array(
						'dataType'=>'json',
						'success'=>'js:function(data,status){
							if ("success" == data) {
								$("#img_block").html("");
							}
						}'
					);
				?>
				<?php echo GxHtml::ajaxLink(UserModule::t('Удалить логотип'), $this->createUrl('/user/myfirms/deletelogo', array('id'=>$model->id)), $imgajax); ?>
			</div>
		<?php endif;?>
		<?php echo $form->activeFileField($model, 'logo'); ?>
		<span class="help-inline"><?php echo $form->error($model,'logo'); ?></span>
	</div>
</div>
<?php endif;?>
<div class="control-group">
	<div class="controls">
		<?php echo GxHtml::submitButton(UserModule::t('Сохранить'), array('class'=>'btn')); ?>
	</div>
</div>
<?php $this->endWidget(); ?>

<?php 
    $max_images_count = ($model->firmTmpl) ? intval($model->firmTmpl->img_count) : 0; 
    $images_count = 0;
    if ($model->firmMedias) {
        $images_count = count($model->firmMedias);
    }
?>
<?php if (0 < $images_count) : ?>
    <div class="images">
        <ul class="images_list">
            <?php foreach($model->firmMedias as $image):?>
                <?php if (1 != $image->media_type) continue;?>
                <li><?php echo GxHtml::image($this->getFirmImgSrc($image->name, 'firms_img_thumb'), GxHtml::encode($image->title), array()) ?></li>
            <?php endforeach;?>
        </ul>
    </div>
<?php endif; ?>


<?php if (0 < $model->id && 0 < $max_images_count && ($images_count < $max_images_count)): ?>
<div class="firm_img_data">

    <div class="images_uploader">
    
<?php 

$this->widget('application.extensions.swfupload.CSwfUpload', array(
    'jsHandlerUrl'=> $this->getBaseUrl().'/js/swfuploader_handlers.js',
    'postParams'=>array(
         'PHPSESSID' => Yii::app()->session->getSessionId(),
    ),
    'config'=>array(
        //'file_post_name' => 'Filedata',
        'use_query_string'=>false,
        //'upload_url'=>  GxHtml::normalizeUrl($this->createUrl('/user/myfirms/uploadimage')), //Use $this->createUrl method or define yourself
        'upload_url'=> $this->getBaseUrl().$this->createUrl('/user/myfirms/uploadimage', array('fid'=>$model->id)), 
        'file_size_limit'=>'20 MB',
        'debug' => false,
        'file_types'=>'*.jpg;*.png;*.gif',
        'file_types_description'=>'Image Files',
        'file_upload_limit'=>($max_images_count - $images_count),
        'file_queue_error_handler'=>'js:fileQueueError',
        'file_dialog_complete_handler'=>'js:fileDialogComplete',
        'upload_progress_handler'=>'js:uploadProgress',
        'upload_error_handler'=>'js:uploadError',
        'upload_success_handler'=>'js:uploadSuccess',
        'upload_complete_handler'=>'js:uploadComplete',
        'custom_settings'=>array('upload_target'=>'divFileProgressContainer'),
        'button_placeholder_id'=>'swfupload',
        'button_width'=>170,
        'button_height'=>20,
        'button_text'=>'<span class="button">'.Yii::t('messageFile', 'Загрузить').'</span>',
        'button_text_style'=>'.button { font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif; font-size: 11pt; text-align: center; }',
        'button_text_top_padding'=>0,
        'button_text_left_padding'=>0,
        'button_window_mode'=>'js:SWFUpload.WINDOW_MODE.TRANSPARENT',
        'button_cursor'=>'js:SWFUpload.CURSOR.HAND',
        ),
    )
);

?>       

        <div class="swfupload"  style="display: inline; border: solid 1px #7FAAFF; background-color: #C5D9FF; padding: 2px;">
           <span id="swfupload"></span>
        </div>
        <div id="divFileProgressContainer" style="height: 75px;"></div>
        <div id="thumbnails"></div>
    </div>
    <div class="images_list"></div>
    <div id="geo-map">
      <?php if (null != $model->longitude && null != $model->latitude) {
          $this->widget('ext.YandexMaps.YandexMap',array(
              'id'=>'map',
              'width'=>'100%',
              'height'=>400,
              'center'=>array($model->latitude, $model->longitude),
              'zoom' => 17,
              'type'=> 'yandex#hybrid',
              'controls' => array(
                  'zoomControl' => true,
                  'typeSelector' => true,
                  'mapTools' => false,
                  'smallZoomControl' => false,
                  'miniMap' => false,
                  'scaleLine' => false,
                  'searchControl' => false,
                  'trafficControl' => false
              ),            
              'placemark' => array(
                  array(
                      'lat'=>$model->latitude,
                      'lon'=>$model->longitude,
                      'properties'=>array(
                          'balloonContentHeader'=>'header',
                          'balloonContent'=>'1',
                          'balloonContentFooter'=>'footer',
                      ),
                      'options' => array(
                        'draggable'=> true
                      )
                  )
              ),
              /*
              'polyline' => array(
                  array('lat'=>55.80,'lon'=>37.30),
                  array('lat'=>55.80,'lon'=>37.40),
                  array('lat'=>55.70,'lon'=>37.30),
                  array('lat'=>55.70,'lon'=>37.40),
                  'options'=>array(
                      'strokeWidth'=> 5 // ширина линии'
                  )
              ),
              */
          ));
          //Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl . '/js/geo_marker.js', CClientScript::POS_END);
          
$script = "
console.log('here');
console.log('typeof(placemark_0)');
console.log(typeof(placemark_0));
console.log(placemark_0);
if ('undefined' !== typeof(placemark_0)) {
  console.log('asdasd');
  placemark_0.events.add(\"dragend\", function (e) {			
    var coords = this.geometry.getCoordinates();
    savecoordinats(coords);
  }, myPlacemark);


  function savecoordinats(coords){	
    var new_coords = [coords[0].toFixed(4), coords[1].toFixed(4)];	
    console.log(new_coords);
    
    // myPlacemark.getOverlay().getData().geometry.setCoordinates(new_coords);
    // document.getElementById(\"latlongmet\").value = new_coords;
    // document.getElementById(\"mapzoom\").value = myMap.getZoom();
    // var center = myMap.getCenter();
    // var new_center = [center[0].toFixed(4), center[1].toFixed(4)];	
    // document.getElementById(\"latlongcenter\").value = new_center;	
  }
}";
          
          Yii::app()->clientScript->registerScript('marker', $script);
      }
      ?>    
    </div>
</div>
<?php endif;?>